package priv.stones.controller.administrate;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import priv.stones.common.annotation.CommitLog;
import priv.stones.common.exception.StonesException;
import priv.stones.common.utils.Result;
import priv.stones.controller.administrate.shiro.ShiroUtils;
import priv.stones.entity.SysMenu;
import priv.stones.mapper.SchemaMapper;
import priv.stones.service.SysMenuService;

import java.util.List;
import java.util.Properties;

/**
 * @Author: Stones
 * @Description:
 * @Date: 2017/10/10 13:22
 */
@Controller
@RequestMapping("/administrate")
public class HomeController extends BaseController {

    @Autowired
    private SysMenuService sysMenuService;
    @Autowired
    private SchemaMapper schemaMapper;

    @RequestMapping("")
    public String Index(Model model) {
        Properties props = System.getProperties();
        model.addAttribute("osName", props.getProperty("os.name"));
        model.addAttribute("osArch", props.getProperty("os.arch"));
        model.addAttribute("osVersion", props.getProperty("os.version"));
        model.addAttribute("javaVerion", props.getProperty("java.version"));
        model.addAttribute("mysqlVerion", schemaMapper.selectVersion());
        return FreeMarkerViewResult("index", model);
    }

    /**
     * 修改密码
     *
     * @param model
     * @return
     */
    @RequestMapping("/updatepwd")
    public String UpdatePwd(Model model) {
        return FreeMarkerViewResult("admin/updatepwd", model);
    }

    @RequestMapping(value = "/updatepwd/exec", method = RequestMethod.POST)
    @ResponseBody
    public Result doUpdatePwd(String oldPwd, String newPwd, String newConfirmPwd) {
        if (StringUtils.isBlank(oldPwd) || StringUtils.isBlank(newPwd) || StringUtils.isBlank(newConfirmPwd)) {
            throw new StonesException("信息未填写完整");
        }
        if (!newPwd.equals(newConfirmPwd)) {
            throw new StonesException("新密码两次输入不一致");
        }
        oldPwd = ShiroUtils.sha256(oldPwd, currAdmin().getLoginsalt());
        if (!currAdmin().getLoginpwd().equals(oldPwd)) {
            throw new StonesException("原密码错误");
        }
        if (sysAdminService.updatePwd(currAdmin().getId(), newPwd)) {
            ShiroUtils.Logout();
            return Result.Ok();
        } else {
            return Result.Error();
        }
    }

    /**
     * 拥有的菜单
     *
     * @return
     */
    @RequestMapping("/ownmenus")
    @ResponseBody
    public Result OwnMenus() {
        List<SysMenu> ownMenus;
        if (currAdmin().getName().equals(ShiroUtils.SuperAdmin)) {
            ownMenus = sysMenuService.selectList(new EntityWrapper<SysMenu>()
                    .eq("status", 1)
                    .orderBy("sort", true));
        } else {
            ownMenus = sysAdminService.selectOwnMenus(currAdmin().getId());
        }
        JSONArray array = new JSONArray();
        for (SysMenu ownMenu : ownMenus) {
            if (ownMenu.getCategory() < 3) {
                JSONObject object = new JSONObject();
                object.put("id", ownMenu.getId());
                object.put("parentid", ownMenu.getParentid());
                object.put("name", ownMenu.getName());
                object.put("sort", ownMenu.getSort());
                object.put("links", ownMenu.getLinks());
                array.add(object);
            }
        }
        Result result = Result.Ok();
        result.put("menus", array);
        return result;
    }


}
